// ActionScript file
import jpa.domain.CarInfo;

import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.mxml.RemoteObject;
		
		[Bindable]
		public var carInfo:CarInfo;
		private var ro:RemoteObject;

		private function save():void
		{
			carInfo.carCode = carCode.text;
			carInfo.name=carName.text;
			if (carInfo.id == 0)
			{
			ro=Global.getRemoteObject("carInfoController",true,create_resultHandler);
			ro.create(carInfo);
			}
			else
			{
			ro=Global.getRemoteObject("carInfoController",true,update_resultHandler);
			ro.update(carInfo);
			}
				
		}

		private function deleteItem():void
		{
			ro.remove(carInfo);		
		}

		private function create_resultHandler(event:ResultEvent):void
		{
			//carInfo.id = event.result.id;
			dispatchEvent(new CarInfoEvent(CarInfoEvent.CREATED, carInfo));			
		}

		private function update_resultHandler(event:ResultEvent):void
		{
			dispatchEvent(new CarInfoEvent(CarInfoEvent.UPDATED, carInfo));			
		}

		private function remove_resultHandler(event:ResultEvent):void
		{
			dispatchEvent(new CarInfoEvent(CarInfoEvent.DELETED, carInfo));			
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(event.fault.faultDetail);
		}